package com.plantpoint.controller;

import com.plantpoint.service.impl.UtilsServiceImpl;
import com.plantpoint.entity.bean.CommonResult;
import com.plantpoint.entity.bean.Image;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author ATSELI
 * @version 1.0
 * @date 2021/4/24 10:43
 */

@RestController
@RequestMapping("/api/v1/utils")
public class UtilsController {

    private final UtilsServiceImpl service;

    public UtilsController(UtilsServiceImpl service) {
        this.service = service;
    }

    /**
     * 上传盆栽图片接口
     * @param file
     * @param id  盆栽id
     * @return
     * @throws IOException
     */
    @PostMapping("/PImg/{id}")
    public CommonResult<Image> pottedImg(@RequestParam MultipartFile file,
                                         @PathVariable(value = "id") String id,
                                         HttpServletRequest request){

        Image image = service.potted(file, id, request);
        if (image != null){
            return new CommonResult<>(20000, "添加成功", image);
        }else {
            return new CommonResult<>(50001, "添加失败", null);
        }
    }


    /**
     * 标签图片上传
     * @param file
     * @param id 标签id
     * @return
     */
    @PostMapping("/EImg/{id}")
    public CommonResult<Image> entryImg(@RequestParam MultipartFile file,
                                        @PathVariable(value = "id") String id){
        Image entry = service.entry(file, id);
        if (entry != null){
            return new CommonResult<>(20000, "添加成功", entry);
        }else {
            return new CommonResult<>(50001, "添加失败", null);
        }
    }
}
